function onLoad() {
    window.location=("skp:onLoad");
}

function setElementValue(elementName, value) {

  elements = document.getElementsByName(elementName);

  if (elements.length > 0) {

    for (var i = 0; i < elements.length; i++) {
      element = elements[i];

      switch(element.tagName) {
        case 'INPUT':
          switch(element.type.toUpperCase()) {
            case 'TEXT':
              element.value = value;
              break;

            case 'BUTTON':
              element.value = value;
              break;

            case 'CHECKBOX':
              switch(value.toUpperCase()) {
                case 'TRUE':
                  element.checked = true;
                  break;
                case 'FALSE':
                  element.checked = false;
                  break;
                default:
              }
              break;

            case 'RADIO':  // may have broken this when allowing to loop through all elements
              for (var j = 0; j < elements.length; j++) {
                if (elements[j].value == value) {
                  elements[j].checked = true;  // Other radio buttons automatically get unchecked
                  break;
                }
              }
              break;
          }
          break;

        case 'SELECT':
          // SELECT is case sensitive (and probably important to leave it that way)

            element.value = value;  // Only works on Windows

        case 'TEXTAREA':
          element.value = value;
          break;

        case 'BUTTON':
          element.value = value;
          break;
      }

    }
  }
}

function setSelectOptions(elementName, value_string) {
    value_array = value_string.replace(/%comma%/g, ',').split(',');
    
    elements = document.getElementsByName(elementName);
    
    if (elements.length > 0) {
      for (var i = 0; i < elements.length; i++) {
        element = elements[i];
        element.type.toUpperCase()
        
        if (element.tagName == 'SELECT') {
          element.options.length = 0;  // Clear the current options

          for (var i = 0; i < value_array.length; i++) {

            value_array[i] = value_array[i].replace(/%apos%/g, "'");  // Decode any single quotes

            option = new Option(value_array[i], value_array[i], false, false);
            element.add(option);
          }
        }
      }
    }
}

function onChangeElement(element) {

  switch(element.tagName.toUpperCase()) {
    case 'INPUT':
      switch(element.type.toUpperCase()) {
        case 'TEXT':
          value = element.value;
          value = value.replace(/'/g, "\\'");  // Must encode any single quotes
          break;

        case 'RADIO':
          value = element.value;
          value = value.replace(/'/g, "\\'");  // Must encode any single quotes
          break;

        case 'CHECKBOX':
          value = element.value + ',' + element.checked;
          break;
      }
      break;

    case 'SELECT':
      value = element.value;
      value = value.replace(/'/g, "\\'");  // Must encode any single quotes
      break;

    case 'TEXTAREA':
      value = element.value;
      value = value.replace(/'/g, "\\'");  // Must encode any single quotes
      break;
  }

  elementName = element.name;  //  Warning:  The variable 'name' appears to be a reserved word on Mac/Safari

  if (elementName == "") elementName = element.id;
  window.location='skp:on_change_element@' + elementName + ',' + value;

  return(value);
}

function load(elementName) {
    elements = document.getElementsByName(elementName);
    
    if (elements.length > 0) {
        element = elements[0];
        var value = element.value;
    }
    
    window.location='skp:load@' + elementName + ',' + value;
    
    return(value);
}

function callRubyShowModel() {
    window.location=("skp:showVolumes");
}